package com.zx.oa.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.zx.oa.dao.census.CensusDao;
import com.zx.oa.dao.census.CensusDaolpml;
import com.zx.oa.entity.ClassInfo;
import com.zx.oa.entity.Major;
import com.zx.oa.entity.Student;
import com.zx.oa.service.census.CensusService;
import com.zx.oa.service.census.CensusServicelpml;
import com.zx.oa.utils.PageSupport;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/*
晨考成绩统计
 */
@WebServlet("/MoringexamServlet.do")
public class MoringexamServlet extends HttpServlet {
    CensusService censusService=new CensusServicelpml();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String caozou= req.getParameter("param");
        if ("cha".equals(caozou)){
            chaServlet(req,resp);
        }else if ("setResult".equals(caozou)){
            getResultServlet(req,resp);
        }
    }


    private void getResultServlet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        resp.setContentType("text/html;charset=utf-8");
        String result= req.getParameter("censusResult");
        String[] nameIds = req.getParameterValues("nameId");

        int census=Integer.parseInt(req.getParameter("census"));

        boolean flag= censusService.setResult(result,nameIds,census);
        PrintWriter out= resp.getWriter();
        out.print(flag);
        out.flush();
        out.close();
    }

    private void chaServlet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        String major=null;//查询的专业的名字
        String className=null;//查询的班级的名字
        String majorJsp=req.getParameter("majorJsp" );
        String classNameJsp=req.getParameter("classnameJsp" );
        if(null!=majorJsp&&!"".equals(majorJsp)){
            major=majorJsp;
        }
        if(null!=classNameJsp&&!"".equals(classNameJsp)){
            className=classNameJsp;
        }


        String pageIndex= req.getParameter("pageIndex");
        if(pageIndex==null){
            //第一次访问查询的servlet的时候没有pageIndex，赋默认值1
            pageIndex="1";
        }
        int currentPageNo=1;//当前页号
        int pageSize= PageSupport.pageSizeVal;//页面尺寸复制
        currentPageNo=Integer.parseInt(pageIndex);

        CensusDao censusDao=new CensusDaolpml();
        int totalCount=censusDao.getCensusTotalCount(major,className);

        //存储页面信息
        PageSupport pages=new PageSupport();
        //设置页面尺寸
        pages.setPageSize(pageSize);
        //设置页面总记录数（注意此方法计算得到总页面数量）
        pages.setTotalCount(totalCount);
        //获取到总的页数
        int totalPageCount=pages.getTotalPageCount();
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        //将当前页号存储到pages对象
        pages.setCurrentPageNo(currentPageNo);

        int censusMoringWeek = Integer.parseInt(req.getParameter("censusMoringWeek"));
        List<Student> studentList=censusService.getStudentList(major,className,currentPageNo,pageSize,censusMoringWeek);



//        获取到全部的培训专业
        List<Major> majorList=censusService.getMajorAll();
        req.setAttribute("majorList",majorList);

        //获取到全部的班级
        List<ClassInfo> classInfoList=censusService.getClassAll(major);
        req.setAttribute("classInfoList",classInfoList);

        req.setAttribute("majorName",major);
        req.setAttribute("className",className);
        req.setAttribute("pageInfo",pages);
        req.setAttribute("studentList",studentList);

        if (censusMoringWeek==1) {
            req.getRequestDispatcher("jsp/census.jsp").forward(req, resp);
        }else if (censusMoringWeek==2){
            req.getRequestDispatcher("jsp/censusWeek.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
